package com.zysc.processflow.controller;

import com.zysc.processflow.common.base.BaseController;
import com.zysc.processflow.entity.Attachment;
import com.zysc.processflow.entity.Category;
import com.zysc.processflow.service.IAttachmentService;
import com.zysc.processflow.service.impl.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/item/category")
public class CategoryController extends BaseController<Category> {

    @Autowired
    private CategoryService categoryService;

    public CategoryController(CategoryService service) {
        super(service);
    }

    @GetMapping("test")
    @ResponseBody
    public String test(){
        return "test..";
    }
    /**
     * 根据父节点查询子节点
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam(value = "pid",defaultValue = "0") Long pid){
        //try{
            if (pid ==null || pid<0){
                //400：参数不合法
                //return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
                //return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
                return ResponseEntity.badRequest().build();
            }
            List<Category> categories = this.categoryService.queryCategoriesByPid(pid);
            if(CollectionUtils.isEmpty(categories)){
                //404
                //return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
                return ResponseEntity.notFound().build();
            }

            return ResponseEntity.ok(categories);
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}
        //500服务器内部错误,可以不用返回，因为自然返回就是500，所以不用写trycatch
        //return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBid(@PathVariable("bid") Long bid){
        List<Category> categoryServiceByIds = categoryService.queryCategoriesByBid(bid);
        if (categoryServiceByIds == null){
            return ResponseEntity.notFound().build();
        }else {
            return ResponseEntity.ok(categoryServiceByIds);
        }
    }


}
